package com.peng.controller;

import cn.hutool.core.util.IdUtil;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.peng.entities.PayDTO;
import com.peng.result.ResultData;
import com.peng.result.ReturnCodeEnum;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RestController
public class PayAlibabaController {
    @Value("${server.port}")
    private String serverPort;

    @GetMapping(value = "/pay/nacos/{id}")
    public String getPayInfo(@PathVariable("id") Integer id) {
        return "nacos registry, serverPort: "+ serverPort+"\t id"+id;
    }

    //openfeign + sentinel 进行服务降级
    @GetMapping("/pay/nacos/get/{orderNo}")
    @SentinelResource(value = "getPayByorderNo" ,blockHandler = "handlerBlockHandler")
    public ResultData getPayByOrderNo(@PathVariable("orderNo") String orderNo) {
        //模拟从数据库查询出数搏并版值给
        PayDTO payDTO = new PayDTO();
        payDTO.setId(1024);
        payDTO.setOrderNo(orderNo);
        payDTO.setAmount(BigDecimal.valueOf(9.9));
        payDTO.setPayNo("pay: " + IdUtil.fastUUID());
        payDTO.setUserId(1);
        return ResultData.success("查询返回值: " + payDTO);
    }
    public ResultData handlerBlockHandler(@PathVariable( "orderNo") String orderNo, BlockException exception){
        return ResultData.fail(ReturnCodeEnum.RC500.getCode() , "getPayByorderNo服务不可用，"+
                "触发sentine1流控配置规则"+"\t"+"o(m_m)o" );
    }

}

